package smb.service;

import lombok.AllArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import smb.dao.TeacherDao;
import smb.domain.Teacher;
import smb.mapper.TeacherMapper;
import smb.vo.*;

import java.util.List;
import java.util.Optional;

@Service
@AllArgsConstructor
public class TeacherService {
    private JdbcTemplate jdbcTemplate;
    private TeacherDao teacherDao;
    private TeacherMapper teacherMapper;

    public TeacherVo getTeacher(String id) {
        Optional<Teacher> teacher = teacherDao.findById(id);
        if (teacher.isPresent()) {
            return teacherMapper.teacherToTeacherVo(teacher.get());
        } else {
            return null;
        }
    }

    public List<CourseSection> getSections(String id) {
        String sql = "SELECT section.id, section.size, section.semester,\n" +
                "       course.id, course.name\n" +
                "FROM section\n" +
                "JOIN course on section.cou_id = course.id\n" +
                "WHERE section.tea_id = ?\n" +
                "ORDER BY section.semester DESC ";
        return jdbcTemplate.query(sql, new Object[]{id}, new CourseSection.rowMapper());
    }
}
